//
//  ViewController.m
//  添加事件到系统日历
//
//  Created by zhengju on 2016/12/1.
//  Copyright © 2016年 zhengju. All rights reserved.
//

#import "ViewController.h"
#import "ZJEventKit.h"
#import "ZJEKEvent.h"
@interface ViewController ()<ZJEventKitDelegate>
{
    BOOL isTime;
}

@property(nonatomic,strong)ZJEventKit * eventkit;
@property(nonatomic,strong)NSDate * startDate;
@property(nonatomic,strong)NSDate * endDate;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.eventkit = [ZJEventKit eventKit];
    
    [self.eventkit startZJEventKitWithdelegate:self];
    
}
- (IBAction)click:(id)sender {
    //创建事件
    ZJEKEvent * zjEKEvent =  [self.eventkit zjEKEvent];
    zjEKEvent.title = @"明天去约会吧11。。";

    // 获取适当的日期（Get the appropriate calendar）
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // 创建起始日期组件（Create the start date components）
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
    oneDayAgoComponents.day = -1;
    zjEKEvent.startDate = [calendar dateByAddingComponents:oneDayAgoComponents toDate:[NSDate date]options:0];
    
    // 创建结束日期组件（Create the end date components）
    NSDateComponents *oneDayFromNowComponents = [[NSDateComponents alloc] init];
    oneDayFromNowComponents.day = 1;
    zjEKEvent.endDate = [calendar dateByAddingComponents:oneDayFromNowComponents toDate:[NSDate date]options:0];
    
    
    [self.eventkit addEvent:zjEKEvent];
    
}
- (IBAction)valueChange:(UIDatePicker *)sender {
   
    if (!isTime) {
        self.startDate = sender.date;
        [[NSUserDefaults standardUserDefaults]setObject:sender.date forKey:@"start"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }else{
        self.endDate = sender.date;
        [[NSUserDefaults standardUserDefaults]setObject:sender.date forKey:@"end"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
}
- (IBAction)start:(id)sender {
    isTime = NO;
}
- (IBAction)end:(id)sender {
    isTime = YES;
}
- (IBAction)find:(id)sender {
    
    if (!self.startDate) {
        self.startDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"start"];
    }
    if (!self.endDate) {
        self.endDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"end"];
    }
    
    [self.eventkit eventsWithStartDate:self.startDate endDate:self.endDate];
    
}
- (IBAction)delete:(id)sender {
    [self.eventkit deleteEvent];
    
}
- (IBAction)modify:(id)sender {
    [self.eventkit editEvent];
}

#pragma ZJEventKitDelegate
-(void)ZJEventKitAddEventSuccess{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Event Created"
                          message:@"Yay!?"
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];
    [alert show];
    
    NSLog(@"保存成功");
}

- (void)ZJEventKitAddEventFailure{

}
-(void)ZJEventKitAddEventErrorUserCancel{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"不允许添加日历事件"
                          message:@"Yay!?"
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];
    [alert show];
}
#pragma 用户删除
-(void)ZJEventKitAddEventErrorUserDeleteSuccess{
    NSLog(@"删除ok");
}
-(void)ZJEventKitAddEventErrorUserDeleteFailure{
    NSLog(@"删除失败");
}
-(void)ZJEventKitAddEventErrorEditSuccess{
    NSLog(@"修改成功");
}
-(void)ZJEventKitAddEventErrorEditFailure{
    NSLog(@"修改失败");
}
@end
